home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 295_01.zip / BFLPUSH.C < prev    next >
Text File  |  1993-04-22  |  3KB  |  136 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bflpush.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. /*#include <string.h>*/
  9. #include "blkio_.h"
  10.  
  11. /*man---------------------------------------------------------------------------
  12. NAME
  13.      bflpush - push block onto free list
  14.  
  15. SYNOPSIS
  16.      #include <blkio.h>
  17.  
  18.      int bflpush(bp, bn_p)
  19.      BLKFILE *bp;
  20.      const bpos_t *bn_p;
  21.  
  22. DESCRIPTION
  23.      The bflpush function puts the block number pointed to by bn_p
  24.      into the free list of the block file associated with BLKFILE
  25.      pointer bp.  If the value pointed to by bn_p is one block past
  26.      the end of the file, it is accepted but not actually added to the
  27.      list.
  28.  
  29.      bflpush will fail if one or more of the following is true:
  30.  
  31.      [EINVAL]       bp is not a valid BLKFILE pointer.
  32.      [EINVAL]       bn_p is the NULL pointer.
  33.      [EINVAL]       bn_p points to a value less than 1.
  34.      [BEEOF]        bp is empty.
  35.      [BEEOF]        bn_p points to a value farther than
  36.                     one block past the end of file.
  37.      [BENFL]        bp does not have a free list.
  38.      [BENOPEN]      bp is not open for writing.
  39.  
  40. SEE ALSO
  41.      bflpop.
  42.  
  43. DIAGNOSTICS
  44.      Upon successful completion, a value of 0 is returned.  Otherwise,
  45.      a value of -1 is returned, and errno set to indicate the error.
  46.  
  47. NOTES
  48.      To use the free list functions in the blkio library, the first
  49.      element of the file header must be the free list head with type
  50.      bpos_t.  This must be initialized to 0 immediately after the file
  51.      is created and not accessed afterward except using bflpush and
  52.      bflpush.  Also, the block size must be at least sizeof(bpos_t).
  53.  
  54. ------------------------------------------------------------------------------*/
  55. int bflpush(bp, bn_p)
  56. BLKFILE *bp;
  57. CONST bpos_t *bn_p;
  58. {
  59.     bpos_t oldflhno = 0;
  60.     bpos_t newflhno = 0;
  61.     void *buf = NULL;
  62.  
  63.     /* validate arguments */
  64.     if (!b_valid(bp) || (bn_p == NULL)) {
  65.         errno = EINVAL;
  66.         return -1;
  67.     }
  68.     if (*bn_p < 1) {
  69.         errno = EINVAL;
  70.         return -1;
  71.     }
  72.  
  73.     /* check if not open */
  74.     if (!(bp->flags & BIOWRITE)) {
  75.         errno = BENOPEN;
  76.         return -1;
  77.     }
  78.  
  79.     /* check if no free list */
  80.     if ((bp->hdrsize < sizeof(oldflhno)) || (bp->blksize < sizeof(oldflhno))) {
  81.         errno = BENFL;
  82.         return -1;
  83.     }
  84.  
  85.     /* check if header not yet written */
  86.     if (bp->endblk < 1) {
  87.         errno = BEEOF;
  88.         return -1;
  89.     }
  90.  
  91.     /* check if block past endblk */
  92.     if (*bn_p > bp->endblk) {
  93.         errno = BEEOF;
  94.         return -1;
  95.     }
  96.  
  97.     /* check if block is endblk */
  98.     if (*bn_p == bp->endblk) {
  99.         errno = 0;
  100.         return 0;
  101.     }
  102.  
  103.     /* get block number of current free list head */
  104.     if (bgethf(bp, (size_t)0, &oldflhno, sizeof(oldflhno)) == -1) {
  105.         BEPRINT;
  106.         return -1;
  107.     }
  108.  
  109.     /* link to rest of free list */
  110.     newflhno = *bn_p;
  111.     buf = calloc((size_t)1, bp->blksize);
  112.     if (buf == NULL) {
  113.         BEPRINT;
  114.         errno = ENOMEM;
  115.         return -1;
  116.     }
  117.     memcpy(buf, &oldflhno, sizeof(oldflhno));
  118.     if (bputb(bp, newflhno, buf) == -1) {
  119.         BEPRINT;
  120.         free(buf);
  121.         buf = NULL;
  122.         return -1;
  123.     }
  124.     free(buf);
  125.     buf = NULL;
  126.  
  127.     /* write new free list head number to header */
  128.     if (bputhf(bp, (size_t)0, &newflhno, sizeof(newflhno)) == -1) {
  129.         BEPRINT;
  130.         return -1;
  131.     }
  132.  
  133.     errno = 0;
  134.     return 0;
  135. }
  136.